/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Control;

import Model.Shape;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

/**
 *
 * @author PhucAnh
 */
public class Triangle extends Shape{
    Point pointStart;
    Point pointFinish;
    
    int xa,ya,xb,yb,xc,yc;
   
    int listX[] = new int[4];
    int listY[] = new int[4];
    
    public Triangle() {

    }

    public Triangle(Point pointStart, Point pointFinish, Color color, int basicStrok) {
        super(color, basicStrok);
        this.pointStart = pointStart;
        this.pointFinish = pointFinish;
        
        xa = pointStart.getX();
        ya = pointFinish.getY();
        
        xb = (pointStart.getX() + pointFinish.getX())/2;
        yb = pointStart.getY();
        
        xc = pointFinish.getX();
        yc = pointFinish.getY();
        
        listX[0]=xa;
        listX[1]=xb;
        listX[2]=xc;
        listX[3]=xa;
        
        listY[0]=ya;
        listY[1]=yb;
        listY[2]=yc;
        listY[3]=ya;
    }
        public void setPoint2(Point pointFinish){
            this.pointFinish = pointFinish;
        
        xa = pointStart.getX();
        ya = pointFinish.getY();
        
        xb = (pointStart.getX() + pointFinish.getX())/2;
        yb = pointStart.getY();
        
        xc = pointFinish.getX();
        yc = pointFinish.getY();
        
        listX[0]=xa;
        listX[1]=xb;
        listX[2]=xc;
        listX[3]=xa;
        
        listY[0]=ya;
        listY[1]=yb;
        listY[2]=yc;
        listY[3]=ya;
        }
    
    @Override
    public void drawShape(Graphics g2,BufferedImage img)
    {
        Point pointA = new Point(xa, ya);
        Point pointB = new Point(xb, yb);
        Point pointC = new Point(xc, yc);
        
        Graphics2D g= (Graphics2D)g2;
        g.setStroke(new BasicStroke(this.getBasicStroke()));
        g.setColor(this.getColor());
        g.drawPolygon(listX, listY, 4);
    }
    @Override
    public void toMove(int m,int n)
    {
        for(int i=0;i<4;i++){
            listX[i]+=m;
            listY[i]+=n;
        }
    }
    
    public boolean isContain(Point p){
        if(pointFinish.getX()>=pointStart.getX() && pointFinish.getY()>=pointStart.getY()){
            if(p.getX()>= pointStart.getX() && p.getX()<= pointFinish.getX() 
            && p.getY()>= pointStart.getY() && p.getY()<= pointFinish.getY()){
                return true;
            } else {
                return false;
            }
        } else if (pointFinish.getX()<pointStart.getX() && pointFinish.getY()<pointStart.getY()){
            if(p.getX()>= pointFinish.getX() && p.getX()<= pointStart.getX() 
            && p.getY()>= pointFinish.getY() && p.getY()<= pointStart.getY()){
                return true;
            } else {
                return false;
            }
        } else if (pointFinish.getX()>=pointStart.getX() && pointFinish.getY()<=pointStart.getY()){
            if(p.getX()>= pointStart.getX() && p.getX()<= pointFinish.getX() 
            && p.getY()>= pointFinish.getY() && p.getY()<= pointStart.getY()){
                return true;
            } else {
                return false;
            }
        } else {
            if(p.getX()>= pointFinish.getX() && p.getX()<= pointStart.getX() 
            && p.getY()>= pointStart.getY() && p.getY()<= pointFinish.getY()){
                return true;
            } else {
                return false;
            }
        }
    }
}
